package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.print.pojo.pojo.dto.PaasPrintListDTO;
import com.xbongbong.pro.print.pojo.pojo.vo.PaasPrintListVO;
import com.xbongbong.saas.service.PrintMobileService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author guohui.zhang
 * @Title: 移动端支持打印
 * @date 2020/10/1913:36
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/print")
public class MobilePrintController {
    @Resource
    private PrintMobileService printMobileService;



    /**
     * 获取基本信息列表
     * @param paasPrintListDTO 前端入参
     * @param br 校验结果
     * @return 返回
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid PaasPrintListDTO paasPrintListDTO, BindingResult br) {
        XbbResponse<PaasPrintListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaasPrintListVO paasPrintListVO = printMobileService.list(paasPrintListDTO);
                response = new XbbResponse<>(paasPrintListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
